/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.core.api;

import cn.star.framework.core.Constant;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.Serializable;
import lombok.Data;

/**
 * 通用结果<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Data
@JsonInclude(Include.NON_NULL)
public class AppResult<T> implements Serializable {

  /** 错误代码 */
  private String code;

  /** 错误信息 */
  private String message;

  /** 响应数据 */
  private T data;

  /** 额外信息 */
  private Object additional;

  public AppResult() {
  }

  public AppResult(String code) {
    this.code = code;
  }

  public AppResult(String code, T data) {
    this.code = code;
    this.data = data;
  }

  public AppResult(String code, T data, String message) {
    this.code = code;
    this.data = data;
    this.message = message;
  }

  public static <T> AppResult<T> ofSuccess() {
    return new AppResult<>(Constant.API_SUCCESS_CODE);
  }

  public static <T> AppResult<T> ofSuccess(T data) {
    return new AppResult<>(Constant.API_SUCCESS_CODE, data);
  }

  public static <T> AppResult<T> ofSuccessMessage(String message) {
    return new AppResult<>(Constant.API_SUCCESS_CODE, null, message);
  }

  public static <T> AppResult<T> ofFail() {
    return new AppResult<>(Constant.API_FAIL_CODE);
  }

  public static <T> AppResult<T> ofFail(String message) {
    return new AppResult<>(Constant.API_FAIL_CODE, null, message);
  }
}
